package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.domain.SystemPerformance;
import com.example.mall.dto.SystemPerformanceDto;
import com.example.mall.mapper.SystemPerformanceMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.SystemPerformanceQuery;
import com.example.mall.service.ISystemPerformanceService;
import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 系统性能监控表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-16
 */
@Service
public class SystemPerformanceServiceImpl extends ServiceImpl<SystemPerformanceMapper, SystemPerformance> implements ISystemPerformanceService {

    @Autowired
    private SystemPerformanceMapper systemPerformanceMapper;

    @Override
    public PageDTO<SystemPerformanceDto> queryPage(SystemPerformanceQuery systemPerformanceQuery) {
        if (systemPerformanceQuery == null) {
            systemPerformanceQuery = new SystemPerformanceQuery();
        }
        Page<SystemPerformance> page = systemPerformanceQuery.toMpPageByDefaultCreateTimeAsc();
        Page<SystemPerformance> cartPage = lambdaQuery()
                .like(systemPerformanceQuery.getPerformanceId()!= null, SystemPerformance::getPerformanceId, systemPerformanceQuery.getPerformanceId())
                .like(systemPerformanceQuery.getResponseTime()!= null, SystemPerformance::getResponseTime, systemPerformanceQuery.getResponseTime())
                .like(systemPerformanceQuery.getThroughput()!= null, SystemPerformance::getThroughput, systemPerformanceQuery.getThroughput())
                .like(systemPerformanceQuery.getCpuUsage()!= null, SystemPerformance::getCpuUsage, systemPerformanceQuery.getCpuUsage())
                .like(systemPerformanceQuery.getMemoryUsage()!= null, SystemPerformance::getMemoryUsage, systemPerformanceQuery.getMemoryUsage())
                .apply(systemPerformanceQuery.getMonitorTime() != null,
                        "DATE_FORMAT(monitor_time, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        systemPerformanceQuery.getMonitorTime())
                .orderByDesc(SystemPerformance::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, SystemPerformanceDto.class);
    }

    @Override
    public SystemPerformance querySystemPerformanceById(Long id) {
        AssertUtil.isNotNull(id,"唯一标识不能为空");
        return systemPerformanceMapper.selectById(id);
    }

    @Override
    public List<SystemPerformance> querySystemPerformanceList() {
        return this.list();
    }

    @Override
    public void insert(SystemPerformanceDto systemPerformanceDto) {
        SystemPerformance systemPerformance = new SystemPerformance();
        CopyUtils.copyProperties(systemPerformanceDto, systemPerformance);
        systemPerformance.setPerformanceId(CodeGenerateUtils.generateUniqueId());
        systemPerformance.setCreateDate(new Date());
        systemPerformance.setUpdateDate(new Date());
        systemPerformanceMapper.insert(systemPerformance);
    }

    @Override
    public void update(SystemPerformanceDto systemPerformanceDto) {
        AssertUtil.isNotNull(systemPerformanceDto.getPerformanceId(),"唯一标识不能为空");
        SystemPerformance systemPerformance = systemPerformanceMapper.selectById(systemPerformanceDto.getPerformanceId());
        AssertUtil.isNotNull(systemPerformance,"唯一标识不存在");
        CopyUtils.copyProperties(systemPerformanceDto, systemPerformance);
        systemPerformance.setUpdateDate(new Date());
        systemPerformanceMapper.updateById(systemPerformance);
    }

    @Override
    public void delete(Long id) {
        AssertUtil.isNotNull(id,"唯一标识不能为空");
        SystemPerformance systemPerformance = systemPerformanceMapper.selectById(id);
        AssertUtil.isNotNull(systemPerformance,"唯一标识不存在");
        systemPerformanceMapper.deleteById(id);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        AssertUtil.isNotNull(ids,"唯一标识不能为空");
        List<SystemPerformance> systemPerformances = systemPerformanceMapper.selectBatchIds(ids);
        AssertUtil.isNotNull(systemPerformances,"唯一标识不存在");
        systemPerformanceMapper.deleteBatchIds(ids);
    }
}
